﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelCountDown : MonoBehaviour
{
    public ButtonPress loveBtn;
    public Text cdText;
    public GameLevelItem gameLevelItem;
    private bool isButtonDown = false;

    public Image light1;
    public Image light2;

    // Start is called before the first frame update
    void Start()
    {
        loveBtn.OnDownEvent.AddListener(() =>
        {
            OnButtonDown();
        });

        loveBtn.m_ExitEvent.AddListener(() =>
        {
            OnButtonUp();
        });

        cdText.text = string.Empty;

    }

    private void OnButtonDown()
    {
        isButtonDown = true;
        ShowCD5();
    }

    private void OnButtonUp()
    {
        isButtonDown = false;
        cdText.text = string.Empty;
    }
    private void ShowCD5()
    {
        if (isButtonDown)
        {
            cdText.text = "5";
            SoundManager.Instance.PlayAudio(SoundType.CountDown.ToString(), false);
            Invoke("ShowCD4", 1);
        }
    }
    private void ShowCD4()
    {
        if (isButtonDown)
        {
            cdText.text = "4";
            SoundManager.Instance.PlayAudio(SoundType.CountDown.ToString(), false);
            Invoke("ShowCD3", 1);
        }
    }

    private void ShowCD3()
    {
        if (isButtonDown)
        {
            cdText.text = "3";
            SoundManager.Instance.PlayAudio(SoundType.CountDown.ToString(), false);
            Invoke("ShowCD2", 1);
        }
    }

    private void ShowCD2()
    {
        if (isButtonDown)
        {
            cdText.text = "2";
            SoundManager.Instance.PlayAudio(SoundType.CountDown.ToString(), false);
            Invoke("ShowCD1", 1);
        }
    }

    private void ShowCD1()
    {
        if (isButtonDown)
        {
            cdText.text = "1";
            SoundManager.Instance.PlayAudio(SoundType.CountDown.ToString(), false);
            Invoke("ShowCD0", 1);
        }
    }

    private void ShowCD0()
    {
        if (isButtonDown)
        {
            cdText.text = string.Empty;
            light1.enabled = false;
            light2.enabled = true;
            gameLevelItem.TriggerResult(true);

        }
    }



}
